package program;

import java.io.File;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: 小程序：输入查找文件和目录进行查询
 * Author:  LiuTao
 * Version: 1.0
 * Date:    2024-03-28  10:23
 */
public class Demo1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要搜索的文件名：");
        String fileName = scanner.next();
        System.out.println("请输入要查询的目录名：");
        String rootPath = scanner.next();
        File rootFile = new File(rootPath);
        if(!rootFile.isDirectory()) {
            System.out.println("您输入的目录有误！");
            return;
        }

        scnDir(rootFile,fileName);
    }

    private static void scnDir(File rootFile, String fileName) {
        File[] files = rootFile.listFiles();
        if(files == null) {
            return;
        }
        for(File f : files) {
            System.out.println("当前位置:" + f.getAbsolutePath());
            if(f.isFile()) {
                if(fileName.equals(f.getName())) {
                    System.out.println("---------------------------------");
                    System.out.println("找到该文件" + f.getAbsolutePath());
                    System.out.println("---------------------------------");
                }
            }else if(f.isDirectory()) {
                    scnDir(f,fileName);
            }else {

            }
        }
    }


}
